<html>
<head><meta charset="utf-8"><title>Vec::split_off(0) · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Asplit_off(0).html">Vec::split_off(0)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209122068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Asplit_off%280%29/near/209122068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Asplit_off(0).html#209122068">(Sep 04 2020 at 17:45)</a>:</h4>
<p>Would it be worth adding a check to see if the argument to <code>split_off</code> is zero,  something like</p>
<div class="codehilite"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="n">at</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">mem</span>::<span class="n">replace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="nb">Vec</span>::<span class="n">new</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209123625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Asplit_off%280%29/near/209123625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Asplit_off(0).html#209123625">(Sep 04 2020 at 18:01)</a>:</h4>
<blockquote>
<p>After the call, the original vector will be left containing the elements [0, at) with its previous capacity unchanged.</p>
</blockquote>



<a name="209123636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Asplit_off%280%29/near/209123636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Asplit_off(0).html#209123636">(Sep 04 2020 at 18:01)</a>:</h4>
<p>the capacity being the sticking point there</p>



<a name="209124605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Asplit_off%280%29/near/209124605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Asplit_off(0).html#209124605">(Sep 04 2020 at 18:11)</a>:</h4>
<p>Fair enough, but that could be addressed by <code>::with_capacity(self.capacity())</code> instead of <code>::new()</code>, right?</p>



<a name="209144589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Asplit_off%280%29/near/209144589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Asplit_off(0).html#209144589">(Sep 04 2020 at 21:55)</a>:</h4>
<p>it also says "Returns a newly allocated vector...", although its somewhat contrived to imagine code relying on that</p>



<a name="209151865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Asplit_off%280%29/near/209151865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Asplit_off(0).html#209151865">(Sep 05 2020 at 00:12)</a>:</h4>
<p>suppose there are only a few items in your huge buffer, and you intend to split those into a new smaller allocation</p>



<a name="209152280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Asplit_off%280%29/near/209152280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Asplit_off(0).html#209152280">(Sep 05 2020 at 00:22)</a>:</h4>
<p>you can <code>mem::take(&amp;mut vec)</code> if you don't care about allocations</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>